package com.karst.webnotes.chapter02
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	
	internal class FirstPV3DApp extends Sprite
	{
		/** 声明三维场景 */
		private var scene : Scene3D = null ;
		/** 三维视窗 */
		private var viewPort : Viewport3D = null ;
		/** 相机 */
		private var camara : Camera3D = null ;
		/** 基本渲染引擎 */
		private var renderEngine : BasicRenderEngine = null ;
		/** 球体 */
		private var sphere : Sphere = null ;
		
		public function FirstPV3DApp()
		{
			createView() ;
			addEvents() ;
		}

		private function createView() : void
		{
			if( scene == null )
				scene = new Scene3D() ;   //创建三维场景
			
			if( camara == null )
				camara = new Camera3D() ;
			
			if( sphere == null )
				sphere = new Sphere() ;      // 创建球体
			
			if( viewPort == null )
				viewPort = new Viewport3D() ;
			
			if( renderEngine == null )
				renderEngine = new BasicRenderEngine() ;
			
			addChild( viewPort ) ;
			viewPort.x = 100 ;
			viewPort.y = 100 ;
			scene.addChild( sphere ) ;
			renderEngine.renderScene( scene, camara, viewPort ) ;   // 渲染引擎、场景（场景、相机、视窗）
		}

		
		
		
		private function addEvents() : void
		{
			addEventListener( Event.ENTER_FRAME, renderHandler ) ; // 添加加载事件监听
		}
		
		private function renderHandler( event : Event ) : void
		{
			if( sphere )
				sphere.localRotationY += 1 ;
			if( renderEngine )
				renderEngine.renderScene( scene, camara, viewPort ) ;
		}
	}
}